package com.vcit.system.util;

import com.alibaba.excel.EasyExcel;
import com.vcit.system.domain.vo.GraduationGradeVo;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 毕业成绩单Excel导出工具类（EasyExcel版本）
 * 
 * @author vcit
 */
@Component
public class GraduationGradeExcelUtil {
    
    /**
     * 导出毕业成绩单Excel
     * 
     * @param response HTTP响应
     * @param graduationGrade 毕业成绩单数据
     * @param studentName 学生姓名
     * @throws IOException IO异常
     */
    public void exportGraduationGrade(HttpServletResponse response, GraduationGradeVo graduationGrade, String studentName) throws IOException {
        try {
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(studentName + "毕业成绩单.xlsx", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            
            // 读取模板
            ClassPathResource resource = new ClassPathResource("templates/export_grades_template.xlsx");
            InputStream template = resource.getInputStream();
            
            // 准备填充数据
            Map<String, Object> fillData = prepareFillData(graduationGrade);
            System.out.println("填充数据: " + fillData);
            // 使用EasyExcel填充模板
            EasyExcel.write(response.getOutputStream())
                    .withTemplate(template)
                    .sheet()
                    .doFill(fillData);
                    
        } catch (Exception e) {
            throw new IOException("导出Excel失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 准备填充数据
     * 
     * @param graduationGrade 毕业成绩单数据
     * @return 填充数据Map
     */
    private Map<String, Object> prepareFillData(GraduationGradeVo graduationGrade) {
        Map<String, Object> fillData = new HashMap<>();
        
        // 基本信息
        fillData.put("dept", graduationGrade.getDept() != null ? graduationGrade.getDept() : "");
        fillData.put("major", graduationGrade.getMajor() != null ? graduationGrade.getMajor() : "");
        fillData.put("eduYear", graduationGrade.getEduYear() != null ? graduationGrade.getEduYear() : "");
        fillData.put("clazz", graduationGrade.getClazz() != null ? graduationGrade.getClazz() : "");
        fillData.put("studentNo", graduationGrade.getStudentNo() != null ? graduationGrade.getStudentNo() : "");
        fillData.put("name", graduationGrade.getName() != null ? graduationGrade.getName() : "");
        
        // 各学期课程成绩
        fillData.put("firstYearFirstTerm", graduationGrade.getFirstYearFirstTerm());
        fillData.put("firstYearSecondTerm", graduationGrade.getFirstYearSecondTerm());
        fillData.put("secondYearFirstTerm", graduationGrade.getSecondYearFirstTerm());
        fillData.put("secondYearSecondTerm", graduationGrade.getSecondYearSecondTerm());
        fillData.put("thirdYearFirstTerm", graduationGrade.getThirdYearFirstTerm());
        fillData.put("thirdYearSecondTerm", graduationGrade.getThirdYearSecondTerm());
        
        // 综合素质分
        fillData.put("comprehensiveQualityScore", graduationGrade.getComprehensiveQualityScore() != null ? graduationGrade.getComprehensiveQualityScore() : "");
        
        return fillData;
    }
} 